package com.softeem.ossmusic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.ossmusic.comm.R;
import com.softeem.ossmusic.entity.Admin;
import com.softeem.ossmusic.mapper.AdminMapper;
import com.softeem.ossmusic.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * java -> mysql -> web前端 -> javaweb -> spring -> springmvc -> springboot
 */
@Tag(name="管理员管理模块")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Operation(summary = "管理员列表",description = "分页查询管理员列表")
    @Parameters({
            @Parameter(name = "page",description = "页码"),
            @Parameter(name = "limit",description = "数据条数")
    })
    @GetMapping("/admin/list")
    public R list(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer limit){
        IPage<Admin> ipage = new Page<>(page,limit);
        return R.success().setData(adminService.list(ipage));
    }

    @Operation(summary = "管理员添加",description = "添加管理员")
    @PostMapping("/admin/add")
    public R add(@RequestBody Admin admin){
       return adminService.add(admin);
    }
    //http请求方法分类 增加：post   删除：delete  修改：update  查询：get
    @Operation(summary = "管理员修改",description = "根据id修改管理员")
    @PutMapping("/admin/update")
    public boolean update(@RequestBody Admin admin){
        return adminService.updateById(admin);
    }

    @Operation(summary = "管理员删除",description = "根据id删除管理员")
    @Parameters({
            @Parameter(name = "id",description = "管理员id")
    })
    @DeleteMapping("/admin/delete")
    public boolean delete(Integer id){
        return adminService.removeById(id);
    }

    @Operation(summary = "管理员登录",description = "根据用户名和密码登录")
    @Parameters({
            @Parameter(name = "username",description = "用户名"),
            @Parameter(name = "password",description = "密码")
    })
    @GetMapping("/admin/login")
    public R login(String username, String password){
        return adminService.login(username,password);
    }

}
